function User(name, department) {
    var _department = department;
    var _name = name;

    //定义name属性
    Object.defineProperty(this, 'name', {
        value: _name,
        writable: true,
        enumerable: true, 
        configurable: false
    });

    //定义department属性
    Object.defineProperty(this, 'department', {
        get: function() {
            console.log('Retrieving department');
            return _department;
        },
        set: function(newValue) {
            console.log('Updating department value to "' + newValue + '"');
            _department = newValue;
        },
        enumerable: true,
        configurable: true
    })

    //定义函数 - 实质上函数也是个属性
    Object.defineProperty(this, 'greeting', {
        value: function() {
            console.log('Hi, I\'m ' + name + '.');
        },
        enumerable: false,
        configurable: false
    })
}

var user = new User('Sunny', 'Engineering');
console.log(user.department)

user.department = 'Marketing';
user.greeting();

//设置user对象
//因为configurable是false 所以下一句产生异常
/*
Object.defineProperty(user, 'name', {
    enumerable: false
});*/

delete user.name;   //因为configurable是false 所以删不掉 但不产生异常
delete user.department;
for(var prop in user) {
    console.log(prop)
}